package model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import threads.RecordPlayer;


public class MultiRecordPlayer implements Runnable, MenuBoxPlayer {
	
	private Player player;
	private Map<Container, List<PlaylistRecord>> recordMap;
	
	public MultiRecordPlayer(Player player) {
		this.player = player;
		this.recordMap = new HashMap<Container, List<PlaylistRecord>>();
		this.initializeRecordMap();
	}
	
	private void initializeRecordMap() {
		List<PlaylistRecord> records = this.player.getRecords();
		for(PlaylistRecord record : records) {
			if(record.getContainer()!=null) {
				if(recordMap.get(record.getContainer())==null) {
					recordMap.put(record.getContainer(), new ArrayList<PlaylistRecord>());
				}
				recordMap.get(record.getContainer()).add(record);
			}
		}
	}

	public boolean play() {
		for(Container c : this.recordMap.keySet()) {
			final List<PlaylistRecord> records = this.recordMap.get(c);
			Thread containerPlayer = new Thread(new RecordPlayer(records));
			containerPlayer.setName(String.format("containerPlayer(%d)", c.getId()));
			containerPlayer.start();
		}
		return true;
	}
	
	@Override
	public void run() {
		this.play();
	}
	
	public static void main(String[] args) {
		new MultiRecordPlayer(Player.getInstance());
	}
}